﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ABCTile.Models;
using ABCTile.DAL;
using ABCTile.ViewModel;
using PagedList;

namespace ABCTile.Controllers
{ 
    public class UserController : Controller
    {
        private ABCDbContext db = new ABCDbContext();

        //
        /**
         * Get all user data and display them on the screen
         * 10 users are displayed on each screen
         */
        [Authorize]
        public ViewResult Index(int? page)
        {
            var ViewModel = new UserIndexData();
            ViewModel.Users = db.Users.Include(u => u.Role);

            /*Set page number as 1 if request is get*/
            if (Request.HttpMethod != "GET")
            {
                page = 1;
            }

            int pageSize = 10; //set page size, 10 users will be displayed on a screen
            int pageIndex = (page ?? 1) - 0; //set page index

            ViewModel.PagedUsers = ViewModel.Users.ToPagedList(pageIndex, pageSize); //Convert "viewModel.Users" into "PagedList" Type to diplay 10 users on a page

            return View(ViewModel);
        }

        /*
         * "UserIndexForSearchingAndPagingWithAjax" action get search string from view page, compare them with user name, and return results on viewpage.
         * If a user click a "Search" button on User/Index.cshtml page, this action will run.
         * This action runs and returns data asynchronously 
        */
        [Authorize]
        public PartialViewResult UserIndexForSearchingAndPagingWithAjax(string searchString, int? page)
        {
            var ViewModel = new UserIndexData(); //"UserIndexData" is defined in ABCTile/ViewModel
            ViewModel.Users = db.Users.Include(u => u.Role);

            /*Compare searchString and UserName, and save them on viewModel.Users*/
            if (!string.IsNullOrEmpty(searchString))
            {
                ViewModel.Users = ViewModel.Users.Where(m => m.UserFname.ToUpper().Contains(searchString.ToUpper())
                    || m.UserLname.ToUpper().Contains(searchString.ToUpper()));
            }

            /*Set page number as 1 if request is get*/
            if (Request.HttpMethod != "GET")
            {
                page = 1;
            }

            int pageSize = 10; //set page size, 10 users will be displayed on a screen
            int pageIndex = (page ?? 1) - 0; //set page index

            ViewModel.PagedUsers = ViewModel.Users.ToPagedList(pageIndex, pageSize); //Convert "viewModel.Users" into "PagedList" Type to diplay 10 users on a page

            return PartialView(ViewModel);
        }

        //
        // GET: /User/Details/5
        [Authorize]
        public ViewResult Details(string id)
        {
            User user = db.Users.Find(id);
            return View(user);
        }

        
        //
        // GET: /User/Edit/5
        [Authorize]
        public ActionResult Edit(string id)
        {
            User user = db.Users.Find(id);
            ViewBag.RoleID = new SelectList(db.Roles, "RoleID", "RoleName", user.RoleID);
            return View(user);
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult Edit(User user)
        {
            if (ModelState.IsValid)
            {
                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.RoleID = new SelectList(db.Roles, "RoleID", "RoleName", user.RoleID);
            return View(user);
        }

        //
        // GET: /User/Delete/5
        [Authorize]
        public ActionResult Delete(string id)
        {
            User user = db.Users.Find(id);
            return View(user);
        }

        //
        // POST: /User/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {            
            User user = db.Users.Find(id);
            db.Users.Remove(user);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}